import 'dart:io';

import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

class PermissionPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Permission"),
        centerTitle: true,
        actions: <Widget>[
          IconButton(
            icon: const Icon(Icons.settings),
            onPressed: () {
              //打开设置
              openAppSettings();
            },
          )
        ],
      ),
      body: Center(
        child: ListView(
            children: Permission.values
                .where((Permission permission) {
                  if (Platform.isIOS) {
                    return permission != Permission.unknown &&
                        permission != Permission.phone &&
                        permission != Permission.sms &&
                        permission != Permission.storage;
                  } else {
                    return permission != Permission.unknown &&
                        permission != Permission.mediaLibrary &&
                        permission != Permission.photos &&
                        permission != Permission.reminders;
                  }
                })
                .map((Permission permission) =>
                    PermissionWidget(permission))
                .toList()),
      ),
    );
  }
}

class PermissionWidget extends StatefulWidget {
  PermissionWidget(this._permission);

  final Permission _permission;

  @override
  State<StatefulWidget> createState() {
    return _PermissionWidgetState();
  }
}

class _PermissionWidgetState extends State<PermissionWidget> {
  _PermissionWidgetState();

  PermissionStatus _permissionStatus = PermissionStatus.denied;

  @override
  void initState() {
    super.initState();
    _listenForPermissionStatus();
  }

  //检查权限
  void _listenForPermissionStatus() {
    final Future<PermissionStatus> statusFuture = widget._permission.status;

    statusFuture.then((PermissionStatus status) {
      setState(() {
        _permissionStatus = status;
      });
    });
  }
  Color getPermissionColor() {
    switch (_permissionStatus) {
      case PermissionStatus.denied:
        return Colors.red;
      case PermissionStatus.granted:
        return Colors.green;
      default:
        return Colors.grey;
    }
  }

  @override
  Widget build(BuildContext context) {
    return ListTile(
      title: Text(widget._permission.toString()),
      subtitle: Text(
        _permissionStatus.toString(),
        style: TextStyle(color: getPermissionColor()),
      ),
      onTap: () async {
        requestPermission(widget._permission);
      },
    );
  }
  //请求权限
  void requestPermission(Permission permission) {
    final Future<Map<Permission, PermissionStatus>> requestFuture = [permission].request();

    requestFuture
        .then((Map<Permission, PermissionStatus> permissionRequestResult) {
      setState(() {
        _permissionStatus = permissionRequestResult[permission]!;
      });
    });
  }
}
